﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;



namespace HA.Data.DisplayOptions2
{

    /// <summary>
    /// 该类为显示选项模块二次重构时添加的类  
    /// </summary>
    [Serializable]
   
    public class DisplayOption2 : EventProperty,  IObjectDataHelper, ICloneable
    {

        public DisplayOption2()
        {
            this._zMagnification = 1;

            this._doGrid3dData = new DOGrid3dData();
            this._doMapData = new DOMapData();


            //订阅子事件
            //this.ReserveEvents();

           
        }

        //供外部调用的事件  显示选项

        [field: NonSerialized]
        public event EventHandler<DisplayOptionsChangedEventArgs> DisplayOptionsSetupChanged;


        //供外部调用  等值线
        [field: NonSerialized]
        public event EventHandler<ContourSetupChangedEventArgs> ContourSetupChanged;


        //向量

        [field: NonSerialized]
        public event EventHandler  VectorSetupChanged;




        #region 公共属性


        double _zMagnification;

        public double ZMagnification
        {
            get { return _zMagnification; }
            set
            {
                if (_zMagnification == value)
                {
                    return;
                }
                else
                {
                    _zMagnification = value;
                    this.IsNeedUpdateData = true;
                }
                if (this._zMagnification <= 0)
                {
                    this._zMagnification = 1;
                }
            }
        }

        Color _backColor = Color.White;


        /// <summary>
        /// 
        /// 背景色
        /// </summary>
        public Color BackColor
        {
            get { return _backColor; }
            set
            {
                if (_backColor == value)
                {
                    return;
                }
                else
                {
                    _backColor = value;
                    this.IsNeedUpdateData = true;
                }
            }
        }



        DOGrid3dData _doGrid3dData;

        public DOGrid3dData DoGrid3dData
        {
            get { return _doGrid3dData; }
            //set { _doGrid3dData = value; }
        }

    


        DOMapData _doMapData;

        public DOMapData DoMapData
        {
            get { return _doMapData; }
            //set { _doMapData = value; }
        }



        #endregion


        #region 公用方法


        /// <summary>
        /// 外部接口
        /// 
        /// 根据主程序数据更新模块内部数据的状态，例如：是否可见是否需要更新等等。
        /// </summary>
        /// <param name="project"></param>
        public void UpdateDataState(Project project)
        {

            if (project ==null )
            {
                return;
            }


            this.SetIsNeedUpdateDataToFalse();


            this.UpdateData(project);

        
        }



        /// <summary>
        /// 更新数据状态  例如：主程序新添加网格 ，则建立网格数据（如果已经存在了则初始化其 显示状态 为 true）等等。
        /// </summary>
        /// <param name="project"></param>
        protected void UpdateData(Project project)
        {

            if (project == null)
            {
                return;
            }

            //Mapdata
            if (project.MapData != null && project.MapData.ConceptualModelCL != null && project.MapData.ConceptualModelCL.Count > 0)
            {
                if (this._doMapData != null)
                {
                    this._doMapData.ProjAddDataVisibleChanged(true);

                }
                else 
                {
                    this._doMapData = new DOMapData();

                }
            }
            else 
            {
                if (this._doMapData !=null )
                {
                    this._doMapData.ProjDeleteDataVisibleChanged(false);
                }
            }


            //网格数据
            //grid，modflow，particle

            if (project.Grid3dData != null && project.Grid3dData.MainGrid != null)
            {
                //有网格

                //网格
                if (this._doGrid3dData != null)
                {
                    this._doGrid3dData.ProjAddDataVisibleChanged(true );
                }
                else
                {
                    this._doGrid3dData = new DOGrid3dData();
                }

                //modflow 
                //particles
                if (project.MfData != null)
                {
                    if ( this._doGrid3dData.Modflow != null)
                    {
                        this._doGrid3dData.Modflow.ProjAddDataVisibleChanged(true );
                    }
                    else
                    {
                        this._doGrid3dData.Modflow = new DOModFlow();
                    
                    }

                    if (this._doGrid3dData.Particles  != null)
                    {
                        this._doGrid3dData.Particles.ProjAddDataVisibleChanged(true);
                    }
                    else
                    {
                        this._doGrid3dData.Particles = new DOParticles ();

                    }

                }
                else
                {
                    if (this._doGrid3dData .Modflow !=null )
                    {
                        this._doGrid3dData.Modflow.ProjDeleteDataVisibleChanged(false );
                    }
                    if (this._doGrid3dData.Particles  != null)
                    {
                        this._doGrid3dData.Particles.ProjDeleteDataVisibleChanged(false);
                    }
                }           

            }
            else 
            {
                //没有网格

                if (this._doGrid3dData !=null)
                {
                    this._doGrid3dData.ProjDeleteDataVisibleChanged(false );
                }


                //modflow 
                //particles
                if (project.MfData != null)
                {
                    if (this._doGrid3dData.Modflow != null)
                    {
                        this._doGrid3dData.Modflow.ProjDeleteDataVisibleChanged(false);
                    }
                    else
                    {
                        this._doGrid3dData.Modflow = new DOModFlow();

                    }

                    if (this._doGrid3dData.Particles != null)
                    {
                        this._doGrid3dData.Particles.ProjDeleteDataVisibleChanged(false);
                    }
                    else
                    {
                        this._doGrid3dData.Particles = new DOParticles();

                    }

                }
                else
                {
                    if (this._doGrid3dData.Modflow != null)
                    {
                        this._doGrid3dData.Modflow.ProjDeleteDataVisibleChanged(false);
                    }
                    if (this._doGrid3dData.Particles != null)
                    {
                        this._doGrid3dData.Particles.ProjDeleteDataVisibleChanged(false);
                    }
                }  


            
            }

        
        }


        /// <summary>
        /// 所有数据的 标志位  IsNeedUpdateData  置为false
        /// </summary>
        public override void SetIsNeedUpdateDataToFalse()
        {
            base.SetIsNeedUpdateDataToFalse();

            this.isNeedUpdateData = false;

            if (this._doGrid3dData !=null )
            {
                this._doGrid3dData.SetIsNeedUpdateDataToFalse();
            }

            if (this._doMapData !=null )
            {
                 this._doMapData.SetIsNeedUpdateDataToFalse();
            }

           


        }


        #region

        /// <summary>
        /// 添加数据  更新视图显示
        /// </summary>
        /// <param name="addDisplayOptionType"></param>
        /// <param name="subTabType"></param>
        public void ProjAddDataChanged(DisplayOptionDataSetType addDisplayOptionType, string subTabType)
        {
            try
            {


                switch (addDisplayOptionType)
                {
                    case DisplayOptionDataSetType.Mesh2DData:
                        break;
                    case DisplayOptionDataSetType.Grid2DData:
                        break;
                    case DisplayOptionDataSetType.ScatterPoint2DData:
                        break;
                    case DisplayOptionDataSetType.Mesh3DData:
                        break;
                    case DisplayOptionDataSetType.Grid3DData:

                        if (this._doGrid3dData != null)
                        {
                            this._doGrid3dData.ProjAddDataVisibleChanged(true);


                            #region //判断是否对子选项卡对应的数据进行了修改  例如：在网格下添加了 Modflow 项目

                            if (DOGrid3DDataSubTabType.ModFlow.ToString() == subTabType.Trim())
                            {
                                if (_doGrid3dData.Modflow != null)
                                {
                                    _doGrid3dData.Modflow.ProjAddDataVisibleChanged(true);
                                }
                                else
                                {
                                    _doGrid3dData.Modflow = new DOModFlow();
                                }
                                if (_doGrid3dData.Particles  != null)
                                {
                                    _doGrid3dData.Particles.ProjAddDataVisibleChanged(true);
                                }
                                else
                                {
                                    _doGrid3dData.Particles = new DOParticles ();
                                }

                            }
                            if (DOGrid3DDataSubTabType.Mt3d.ToString() == subTabType.Trim())
                            {
                                //if (_doGrid3dData.Modflow != null)
                                //{
                                //    _doGrid3dData.Modflow.ProjDataVisibleChanged(true);
                                //}
                                //else
                                //{
                                //    _doGrid3dData.Modflow = new DOModFlow();
                                //}
                            }

                            #endregion

                        }
                        else
                        {
                            this._doGrid3dData = new DOGrid3dData();
                        }

                        break;
                    case DisplayOptionDataSetType.ScatterPoint3DData:
                        break;
                    case DisplayOptionDataSetType.MapData:

                        if (this._doMapData != null)
                        {
                            this._doMapData.ProjAddDataVisibleChanged(true);

                            //目前只有一个字选项卡
                        }
                        else
                        {
                            this._doMapData = new DOMapData();
                        }



                        break;
                    case DisplayOptionDataSetType.Materials:
                        break;
                    case DisplayOptionDataSetType.LightingOptions:
                        break;
                    case DisplayOptionDataSetType.Axes:
                        break;
                    case DisplayOptionDataSetType.DrawingGrid:
                        break;
                    default:
                        break;

                }
            }
            catch
            {


            }
        }

        /// <summary>
        /// 删除数据 更新视图显示
        /// </summary>
        /// <param name="addDisplayOptionType"></param>
        /// <param name="subTabType"></param>
        public void ProjDeleteDataChanged(DisplayOptionDataSetType addDisplayOptionType, string subTabType)
        {

            switch (addDisplayOptionType)
            {
                case DisplayOptionDataSetType.Mesh2DData:
                    break;
                case DisplayOptionDataSetType.Grid2DData:
                    break;
                case DisplayOptionDataSetType.ScatterPoint2DData:
                    break;
                case DisplayOptionDataSetType.Mesh3DData:
                    break;
                case DisplayOptionDataSetType.Grid3DData:

                    if (this._doGrid3dData != null)
                    {

                        if (DOGrid3DDataSubTabType.nothing.ToString() == subTabType.Trim())
                        {
                            if (this._doGrid3dData != null)
                            {
                                this._doGrid3dData.ProjDeleteDataVisibleChanged(false);
                            }
                        }


                        if (DOGrid3DDataSubTabType.ModFlow.ToString() == subTabType.Trim())
                        {
                            if (_doGrid3dData.Modflow != null)
                            {
                                _doGrid3dData.Modflow.ProjDeleteDataVisibleChanged(false);
                            }
                           
                            if (_doGrid3dData.Particles != null)
                            {
                                _doGrid3dData.Particles.ProjDeleteDataVisibleChanged(false);
                            }
                        }
                        if (DOGrid3DDataSubTabType.Mt3d.ToString() == subTabType.Trim())
                        {
                            //if (_doGrid3dData.Modflow != null)
                            //{
                            //    _doGrid3dData.Modflow.ProjDataVisibleChanged(true);
                            //}
                            //else
                            //{
                            //    _doGrid3dData.Modflow = new DOModFlow();
                            //}
                        }


                    }

                    break;
                case DisplayOptionDataSetType.ScatterPoint3DData:
                    break;
                case DisplayOptionDataSetType.MapData:

                    if (this._doMapData !=null )
                    {
                        //因为目前只有一个选项卡  所以不需要对string 进行判断了
                        this._doMapData.ProjDeleteDataVisibleChanged(false );

                    }



                    break;
                case DisplayOptionDataSetType.Materials:
                    break;
                case DisplayOptionDataSetType.LightingOptions:
                    break;
                case DisplayOptionDataSetType.Axes:
                    break;
                case DisplayOptionDataSetType.DrawingGrid:
                    break;
                default:
                    break;

            }

        }

        #endregion

        public void DeleteAll()
        { 
        
        
        
        }


        /// <summary>
        /// 订阅模块中子对象的事件   
        /// </summary>
        public void ReserveEvents()
        {

            this.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);

            if (this._doGrid3dData != null)
            {

                this._doGrid3dData.Grid3d.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);

                if (this._doGrid3dData != null && this._doGrid3dData.Modflow != null)
                {
                    this._doGrid3dData.Modflow.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);
                }

                if (this._doGrid3dData != null && this._doGrid3dData.Particles != null)
                {
                    this._doGrid3dData.Particles.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);
                }
            }
            if (this._doMapData  !=null && this._doMapData .Map !=null  )
            {
                this._doMapData  .Map   .DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);      
            }



            if (this._doGrid3dData !=null )
            {
                if (this._doGrid3dData .Grid3d .Contour !=null)
                {

                    this._doGrid3dData.Grid3d.Contour.ContourSetupChanged += new EventHandler<ContourSetupChangedEventArgs>(ContourSetupChangedChanged);
                    this._doGrid3dData.Grid3d.Contour .ContourLineProperty .LineScalarBar .ContourSetupChanged += new EventHandler<ContourSetupChangedEventArgs>(ContourSetupChangedChanged);
                    this._doGrid3dData.Grid3d.Contour.ContourLineProperty.LineTagging .ContourSetupChanged += new EventHandler<ContourSetupChangedEventArgs>(ContourSetupChangedChanged);

                }

            }
            if (this._doGrid3dData !=null && this._doGrid3dData .Grid3d .Vector !=null )
            {

                this._doGrid3dData.Grid3d.Vector .VectorsSetupChanged  += new EventHandler(VectorSetupChangedChanged );

            }

        }
        /// <summary>
        /// 取消模块中对子对象事件的订阅
        /// </summary>
        public void DeserveEvents()
        {
            this.DisplayOptionsChanged -= new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);

            if (this._doGrid3dData != null)
            {

                this._doGrid3dData.Grid3d.DisplayOptionsChanged -= new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);

                if (this._doGrid3dData != null && this._doGrid3dData.Modflow != null)
                {
                    this._doGrid3dData.Modflow.DisplayOptionsChanged -= new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);
                }

                if (this._doGrid3dData != null && this._doGrid3dData.Particles != null)
                {
                    this._doGrid3dData.Particles.DisplayOptionsChanged -= new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);
                }
            }
            if (this._doMapData != null && this._doMapData.Map != null)
            {
                this._doMapData.Map.DisplayOptionsChanged -= new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOptionsSetupChangedChanged);
            }



            if (this._doGrid3dData != null)
            {
                if (this._doGrid3dData.Grid3d.Contour != null)
                {

                    this._doGrid3dData.Grid3d.Contour.ContourSetupChanged -= new EventHandler<ContourSetupChangedEventArgs>(ContourSetupChangedChanged);
                   
                    this._doGrid3dData.Grid3d.Contour.ContourLineProperty.LineScalarBar.ContourSetupChanged -= new EventHandler<ContourSetupChangedEventArgs>(ContourSetupChangedChanged);
                    
                    this._doGrid3dData.Grid3d.Contour.ContourLineProperty.LineTagging.ContourSetupChanged -= new EventHandler<ContourSetupChangedEventArgs>(ContourSetupChangedChanged);

                }

            }

            if (this._doGrid3dData != null && this._doGrid3dData.Grid3d.Vector != null)
            {

                this._doGrid3dData.Grid3d.Vector.VectorsSetupChanged -= new EventHandler(VectorSetupChangedChanged);

            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DisplayOptionsSetupChangedChanged(object sender, DisplayOptionsChangedEventArgs e)
        {

            if (this.DisplayOptionsSetupChanged != null)
            {
                this.DisplayOptionsSetupChanged(sender, e);
            }
        }


        /// <summary>
        /// 等值线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ContourSetupChangedChanged(object sender, ContourSetupChangedEventArgs e)
        {

            if (this.ContourSetupChanged  != null)
            {
                this.ContourSetupChanged(sender, e);
            }
        }

        /// <summary>
        /// 向量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void VectorSetupChangedChanged(object sender, EventArgs  e)
        {
            if (this.VectorSetupChanged != null)
            {
                this.VectorSetupChanged(sender, e);
            }
        
        }

        #endregion


        #region IObjectDataHelper 成员



        /// <summary>
        /// 说明
        /// 判断对应的选项改变，并作出对应的更新动作  具体到改程序则对应“显示选项”主窗体的 OK 按钮需要更新的数据
        /// </summary>
        /// <param name="obj">副本数据</param>
        public void ResetDataAndTriggerEvent(object obj)
        {
            if (obj == null)
            {
                return;
            }

            DisplayOption2 doSetup = (DisplayOption2)obj;

            if (doSetup .IsDataChanged () ==false )
            {
                return;
            }

            try
            {

                List<string> commonChanges = new List<string>();

                if (this._backColor != doSetup.BackColor)
                {
                    this.BackColor = doSetup.BackColor;
                    commonChanges.Add(DOCommonType.BackColor.ToString());
                }

                if (this.ZMagnification != doSetup.ZMagnification)
                {
                    this.ZMagnification = doSetup.ZMagnification;
                    commonChanges.Add(DOCommonType.ZMagnification.ToString());
                }

                if (commonChanges.Count > 0)
                {
                    this.AskDisplayOptionChanged(new DisplayOptionsChangedEventArgs(this, DisplayOptionsType.Common, commonChanges));
                }


                //其他的属性 。。。
                if (this._doGrid3dData != null)
                {
                    this._doGrid3dData.ResetDataAndTriggerEvent(doSetup.DoGrid3dData);
                }
                if (this._doMapData != null)
                {
                    this._doMapData.ResetDataAndTriggerEvent(doSetup.DoMapData);
                }

            }
            catch
            {


            }

            //throw new NotImplementedException();
        }


        /// <summary>
        /// 说明
        /// 判断类中所有数据（包括作为其字段的类的实例（对象）的数据状态）是否改变  不包括需要单独走事件的数据（例如：向量DOVector，等值线DOContour）的改变状态
        /// </summary>
        /// <returns></returns>
        public bool IsDataChanged( )
        {
            if (this==null )
            {
                return false;
            }

            if (this.IsNeedUpdateData == true)
            {
                return true;
            }
            else
            {
                if ((_doGrid3dData != null && _doGrid3dData.IsDataChanged() == true) || (_doMapData != null && _doMapData.IsDataChanged() == true))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            //return true;
            //throw new NotImplementedException();

        }




        #endregion


        #region 克隆

        public object Clone()
        {
            DisplayOption2 obj = new DisplayOption2();

            obj = (DisplayOption2)this.MemberwiseClone();

            if (this._doGrid3dData != null)
            {               
                obj._doGrid3dData = (DOGrid3dData)this._doGrid3dData.Clone();
            }
            else
            {
                obj._doGrid3dData = null;
            }

            if (this._doMapData != null)
            {
                
                obj._doMapData = (DOMapData)this._doMapData.Clone();
            }
            else
            {
                obj._doMapData = null;
            }

            return obj;

        }

        #endregion




        #region 暂时不使用

        ///// <summary>
        ///// 判断对应的选项改变，并作出对应的更新动作
        ///// 
        ///// </summary>
        ///// <param name="doSetup"></param>
        //public void ResetDisplayOption(DisplayOption2 doSetup)
        //{
        //    //存储所有发生变化的标志（字符串？有点安全隐患）因为这为重构，为了尽量较小对第一次的影响，暂时仍使用字符串吧
        //    List<string> commonChanges = new List<string>();
        //    if (this._backColor != doSetup.BackColor)
        //    {
        //        this.BackColor = doSetup.BackColor;
        //        commonChanges.Add(DisplayOptionChangedLabel.BackColor.ToString());
        //    }
        //    if (this.ZMagnification != doSetup.ZMagnification)
        //    {
        //        this.ZMagnification = doSetup.ZMagnification;
        //        commonChanges.Add(DisplayOptionChangedLabel.ZMagnification.ToString());
        //    }
        //    if (commonChanges.Count > 0)
        //    {
        //        this.AskDisplayOptionChanged(new DisplayOptionsChangedEventArgs(this, DisplayOptionsType.Common, commonChanges));
        //    
        //    //其他的属性 。。。
        //    this._doGrid3dData.ResetDOGrid3dData(doSetup.DoGrid3dData);
        //    this._doMapData.ResetDOMapData(doSetup.DoMapData);
        //}

        //#region 事件模型

        //[field: NonSerialized]
        //public event EventHandler<DisplayOptionsChangedEventArgs> DisplayOptionsChanged;

        //public void AskDisplayOptionChanged(DisplayOptionsChangedEventArgs e)
        //{
        //    if (DisplayOptionsChanged != null)
        //    {
        //        DisplayOptionsChanged(this, e);

        //    }
        //}


        //#endregion

        #endregion





    }
}
